<template>
	<view class="page" @touchmove.stop.prevent="moveHandle">
		<view class="top">
			<image src="/static/img/logo.png" class="user"></image>
			<text class="login">登录</text>
			<view class="tel">
				<input type="number" name="phone" id="phone" @input="inputPhone" placeholder="请填写手机号码" />
			</view>
			<view class="checked">
				<input type="number" name="code" id="code" @input="inputCode" placeholder="请填写验证码" />
				<text @click="toCode" v-if="show">获取验证码</text>
				<text v-if="!show">{{count}}秒后重新发送</text>
			</view>
		</view>
		<view class="login-now" @click="toHome">
			<image src="/static/img/login.png"></image>
			<text>立即登录</text>
		</view>
		<text class="register" @click="openAuthority">去注册</text>
		<view class="bottom-img">
			<image src="/static/img/login-bottom.png"></image>
		</view>
		<Authority v-if='showAuthority' :closeBtn='closeAuthority' :btnFunction='authorityFun'></Authority>
	</view>
</template>

<script>
	import Authority from '@/components/authorityPopup.vue'
	export default {
		//页面加载执行 获取前页传过来的参数
		onLoad: function(options) {
		
			//获取用户地址
			this.getLocationAddress()
		},
		onshow(){
			this.getLocationAddress()
		},
		data() {
			return {
				count: '',
				show: true,
				canBack:false,
				showAuthority:false,
				province:'',
				city:'',
				area:'',
				lon:'',
				lat:'',
			}
		},
		components: {
		
			Authority
		},
		methods: {
			moveHandle() {
				return;
			},
			//获取经纬度
			getLocationAddress(){
				let _this = this;
				//#ifdef APP-PLUS
				plus.geolocation.getCurrentPosition( function(position){
				// plus.nativeUI.alert(JSON.stringify(position));
				var info = position.addresses;
				 _this.lat = position.coords.latitude;
				 _this.lon = position.coords.longitude;
				 
				console.log( _this.lat,position.coords,info,'--------------------------------获取经纬度')
				
				}, function ( e ) {
				plus.nativeUI.alert( "获取定位位置信息失败："+e.message );
				},{geocode:true});
				//#endif
			},
			//打开授权弹框
			openAuthority(){
			
				let memberInfo = uni.getStorageSync('member')
				console.log(memberInfo,'用户信息')
				if(memberInfo==null||memberInfo==''|| typeof(memberInfo) =='undefined'){
					this.showAuthority = true
				}else{
					//判断授权之后  是否有手机号
						/* uni.showToast({
						   title:'正前往注册！' ,	
						   icon:'none',
						   duration:1000
						})
						setTimeout(function(){
							
							//跳转到注册页
							uni.navigateTo({
								url: '/pages/register/Register'
							})
						},1000) */
						//跳转到注册页
						uni.navigateTo({
							url: '/pages/register/Register'
						})
						return
					// }
				}
				
			},	
			//关闭授权弹框
			closeAuthority(){
				this.showAuthority = false
			},
			authorityFun(){
				var that = this;
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						//支持微信、qq和微博等
						if (~res.provider.indexOf('weixin')) {
							uni.showLoading({
							    title: '授权中'
							})
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									// 获取用户信息
									uni.getUserInfo({
										provider: 'weixin',
										success: function(infoRes) {
											uni.hideLoading();
											console.log("App微信获取用户信息成功", infoRes.userInfo);
											that.showAuthority = false
										
											
											console.log(infoRes.userInfo.unionId,'登录unionId')
											//在这请求获取用户信息的接口 手机号 有返回值存member 没有 则存 微信返回的
											that.$api.getUnidMember({openId_app:infoRes.userInfo.openId,unionid:infoRes.userInfo.unionId}).then(res => {
												console.log(res)
												console.log(res.data)
												console.log(res.data.phone,'授权获取用户手机号')
												console.log(res.data.minfo,'状态')
												if(res.data.status == true){
													
													if(res.data.minfo == true){
														if(res.data.data.phone==null||res.data.data.phone==''||typeof(res.data.data.phone)=='undefined'){
															//注册页
															uni.navigateTo({
																url:'/pages/register/Register'
															}) 
														}else{
															//数据库中的 可能有phone 也能 没有 但一定有 头像 昵称 unionid
															uni.setStorage({
																key: "member",
																data: res.data.data
															})
															uni.setStorage({
																key: "token",
																data: res.data.data.token
															})
															//首页 有手机号跳首页
															uni.switchTab({
																url:'/pages/index/index'
															})
														}
													}else{
														//微信返回的 没有phone
														uni.setStorage({
															key: "member",
															data: infoRes.userInfo
														})
														//注册
														uni.navigateTo({
															url:'/pages/register/Register'
														}) 
													}
													
												}
												
											
												
											})
										}
									});
								
								},
								fail: function(res) {
									console.log("App微信获取用户信息失败", res);
								}
							})
						}
					}
				});
			},
		
			
			
			//注册
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/Register'
				})
			},
			
			//立即登录 登录成功之后跳到首页
			toHome() {
				var cid = uni.getStorageSync("cid");
				console.log(cid,'------------------------------cicic=---------')
				var _member = uni.getStorageSync("member")
				console.log(_member);
				var that = this
				this.$api.getLogin({
					phone: this.phone,
					verify: this.code,
					openId_app:_member.openId,
					unionId:_member.unionId,
					name:_member.nickName,
					headpic:_member.avatarUrl,
					client_id:cid,
					lon:this.lon,
					lat:this.lat,
					
				}).then(res => {
					if (res.data.status == true) {
						//提示信息
						uni.showToast({
							title: res.data.msg,
						});
						if(_member.avatarUrl != res.data.data.token.avatarUrl||_member.nickName != res.data.data.token.nickName){
							console.log('用户登录修改信息')
							//更新用户数据 接口
							// this.getUpdate();
							this.$api.getUdateInfo({
								phone: this.phone,
								name:_member.nickName,
								headpic:_member.avatarUrl,
								
							}).then(result => {
								console.log(result,'-------------193193193--------')
								if (result.data.status == true) {
									console.log('---------------------')
									//存 手机号登录返回的 不存修改过的 用户缓存 头像 和昵称 
									uni.setStorage({
										key: "member",
										data: res.data.data.token
									})
								}
							})
							
						}else{
							console.log('---------------------else 11111111111111111111111111111111')
							//存 用户缓存 头像 和昵称
							uni.setStorage({
								key: "member",
								data: res.data.data.token
							})
						}
						
						//获取token  把token 存到缓存
						uni.setStorage({
							key: "token",
							data: res.data.data.token.token,
							success() {
								that.canBack = true
							
								setTimeout(function(){
										//跳转到首页
										var pages = getCurrentPages(); // 当前页面
										var beforePage = pages[pages.length - 2]; // 前一个页面
										var _count = 0
										for (let i = pages.length-1; i >= 0; i--) {
											console.log(pages[i].route)
											if(pages[i].route!="pages/login/Login"&&pages[i].route!="pages/register/Register"){
												
												uni.navigateBack({
													delta: _count
												})
												return
											}else{
												_count++
											}
										}
									},1000)
								
							}
						})

						console.log(res.data.data.token);
					}
				})

			},
			
			//获取验证码
			toCode() {
				this.$api.getCode({
					phone: this.phone,
					mark:'login',
				}).then(res => {
					if (res.data.status == true) {
						this.show = false;
						uni.showToast({
							title: res.data.msg
						})
						const time_count = 60;
						if (!this.timer) {
							this.count = time_count;
							this.timer = setInterval(() => {
								if (this.count > 0 && this.count <= time_count) {
									this.count--;
								} else {
									this.show = true;
									clearInterval(this.timer);
									this.timer = null;
								}
							}, 1000)
						}
						
					} else {
						uni.showToast({
							title: '发送失败'
						})
					}
				})
			},
			//实时获取输入的验证码
			inputCode: function(event) {
				this.code = event.target.value
				
			},
			//实时获取输入的手机号
			inputPhone: function(event) {
				this.phone = event.target.value
				
			},
		},
		onBackPress(options) {
		    // 这里可以自定义返回逻辑，比如下面跳转其他页面
		   if(this.canBack == false){
			   // return true 表示禁止默认返回
			   return true
		   }else{
			   return false
		   }
		},
		
	}
</script>

<style>
	.page {
		width: 100%;
		height: 100vh;
	}

	.top {
		width: 750rpx;
		height: 848rpx;
	}

	.user {
		width: 162rpx;
		height: 162rpx;
		margin-left: 294rpx;
		margin-top: 242rpx;
	}

	.login {
		display: block;
		margin-left: 339rpx;
		margin-top: 45rpx;
	}

	.tel {
		width: 630rpx;
		height: 109rpx;
		line-height: 109rpx;
		margin-left: 60rpx;
		margin-top: 45rpx;
		border-bottom: 1rpx solid #999;
	}

	.tel input {
		padding-left: 30rpx;
		width: 100%;
		height: 100%;
	}

	.checked {
		position: relative;
		width: 630rpx;
		height: 108rpx;
		margin-left: 60rpx;
		border-bottom: 1rpx solid #999;
	}

	.checked input {
		padding-left: 30rpx;
		width: 100%;
		height: 100%;
	}

	.checked text {
		position: absolute;
		right: 0;
		top: 45rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		text-decoration: underline;
		color: rgba(254, 177, 71, 1);
	}

	.login-now {
		position: relative;
		z-index: 100;
		width: 386rpx;
		height: 76rpx;
		line-height: 76rpx;
		margin-left: 182rpx;
		margin-top: -20rpx;
	}

	.login-now image {
		width: 386rpx;
		height: 76rpx;
	}

	.login-now text {
		position: absolute;
		left: 126rpx;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
	}

	.bottom-img {
		width: 750rpx;
		height: 776rpx;
		margin-top: -150rpx;
	}

	.bottom-img image {
		width: 750rpx;
		height: 776rpx;
	}

	.register {
		position: relative;
		z-index: 100;
		display: block;
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		text-decoration: underline;
		color: rgba(102, 102, 102, 1);
		margin-left: 323rpx;
		margin-top: 63rpx;
	}
</style>
